#include "CSurface.h"


CSurface::CSurface()
{
	m_pSurface = NULL;
}

CSurface::~CSurface()
{
	Release();
}

//-------------------------------------------------//
// Name : Relase                                   //
// Desc : Release the object , free memory etc.... //
//-------------------------------------------------//
void CSurface::Release()
{
	if(m_pSurface)
		m_pSurface->Release();

	m_pSurface = NULL;
}

//-------------------------------------------------//
// Name : CreateSurfaceFromFile                    //
// Desc : Create and return a surface			   //
//-------------------------------------------------//
HRESULT CSurface::CreateSurfaceFromFile(LPCTSTR FilePath , LPDIRECT3DDEVICE9 pD3dDevice)
{
	HRESULT					hr;
	D3DXIMAGE_INFO			imageInfo;

	hr = D3DXGetImageInfoFromFile( FilePath , &imageInfo );

	if(FAILED(hr))
		return hr;

	hr = pD3dDevice->CreateOffscreenPlainSurface( imageInfo.Width ,
													imageInfo.Height ,
													imageInfo.Format , 
													D3DPOOL_DEFAULT ,
													&m_pSurface ,
													NULL );

	if(FAILED(hr))
		return hr;

	hr = D3DXLoadSurfaceFromFile( m_pSurface , NULL , NULL , FilePath , NULL , D3DX_DEFAULT , 0 , NULL );
	
	if(FAILED(hr))
		return hr;

	return D3D_OK;
}

//--------------------------//
// Name : GetBackBuffer     //
// Desc : Get a back buffer //
//--------------------------//
LPDIRECT3DSURFACE9	CSurface::GetBackBuffer(LPDIRECT3DDEVICE9 pD3dDevice)
{
	LPDIRECT3DSURFACE9	pBackBuffer;
	HRESULT				hr;

	hr = pD3dDevice->GetBackBuffer( 0 , 0 , D3DBACKBUFFER_TYPE_MONO , &pBackBuffer );

	if(FAILED(hr))
		return NULL;

	return pBackBuffer;
}


//-------------------------------------//
// Name : Draw Surface                 //
// Desc : Draw a surface to the Screen //
//-------------------------------------//
void CSurface::DrawSurface(const RECT *scrRect, const RECT *destRect , LPDIRECT3DDEVICE9 pD3dDevice )
{
	pD3dDevice->StretchRect( m_pSurface , scrRect , GetBackBuffer(pD3dDevice) , destRect , D3DTEXF_NONE );
}